package com.softeem.music_sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.music_sys.comm.Result;
import com.softeem.music_sys.entity.Singer;
import com.softeem.music_sys.service.SingerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "歌手接口", description = "歌手相关操作：增删改查等")
@RestController
@RequestMapping("/singer")
public class SingerController {

    @Autowired
    private SingerService singerService;

    // 添加歌手
    @Operation(summary = "添加歌手", description = "添加歌手，提供JSON格式歌手数据")
    @PostMapping
    public Result add(@RequestBody Singer singer) {
        boolean flag = singerService.save(singer);
        return flag ? Result.success().setMsg("添加成功").setData(singer) : Result.fail("添加失败");
    }

    // 修改歌手信息
    @Operation(summary = "修改歌手信息", description = "修改歌手信息，提供JSON格式歌手数据")
    @PutMapping
    public Result update(@RequestBody Singer singer) {
        boolean flag = singerService.updateById(singer);
        return flag ? Result.success().setMsg("修改成功") : Result.fail("修改失败");
    }

    // 删除歌手
    @Operation(summary = "删除歌手", description = "根据歌手ID删除歌手")
    @Parameters({
            @Parameter(name = "id", description = "歌手ID")
    })
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        // 检查歌手是否存在
        Singer singer = singerService.getById(id);
        if (singer == null) {
            return Result.fail("歌手不存在");
        }
        boolean flag = singerService.removeById(id);
        return flag ? Result.success().setMsg("删除成功") : Result.fail("删除失败");
    }

    // 歌手列表查询（分页）
    @Operation(summary = "歌手列表查询", description = "歌手列表查询，支持分页")
    @Parameters({
            @Parameter(name = "page", description = "页码"),
            @Parameter(name = "limit", description = "每页数量")
    })
    @GetMapping("/list")
    public Result list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit
    ) {
        Page<Singer> pageUtil = new Page<>(page, limit);
        List<Singer> list = singerService.list(pageUtil);
        return Result.success().setData(list);
    }

    // 根据ID获取歌手信息
    @Operation(summary = "获取歌手信息", description = "根据歌手ID获取歌手详细信息")
    @Parameters({
            @Parameter(name = "id", description = "歌手ID")
    })
    @GetMapping("/{id}")
    public Result getSingerById(@PathVariable Integer id) {
        Singer singer = singerService.getById(id);
        if (singer == null) {
            return Result.fail("歌手不存在");
        }
        return Result.success().setData(singer);
    }

    // 根据歌手类型查询歌手列表
    @Operation(summary = "根据歌手类型查询歌手", description = "根据歌手类型查询歌手列表")
    @Parameters({
            @Parameter(name = "type", description = "歌手类型")
    })
    @GetMapping("/byType/{type}")
    public Result getSingerByType(@PathVariable String type) {
        List<Singer> list = singerService.list(new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Singer>().eq("type", type));
        return Result.success().setData(list);
    }
}